import UIKit

class TF_LivingMoreConfiguration: UIButton {
    /**提问*/
    @objc public var questionBtn:TF_MoreConfigurationButton?
    /**客服*/
    @objc public var customerServiceBtn:TF_MoreConfigurationButton?
    
    @objc public let tempArray = NSMutableArray()
    @objc public var isShowConfiguration:Bool = false {
            didSet{
                
            self.showConfiguration(value: isShowConfiguration)
                
        }
    }
    //显示提问
    @objc public var questionShow:Bool = false
    //线路切换
    @objc public var liveLineShow:Bool = false
    //个人中心
    @objc public var infoShow:Bool = false
    //红_包
    @objc public var redPackRecordShow:Bool = false
    //【字段含义】提_现
    @objc public var withdrawShow:Bool = false
    //【字段含义】举报开关
    @objc public var reportShow:Bool = false
    //【字段含义】商_城开关
    @objc public var storeShow:Bool = false
    //【字段含义】客服开关
    @objc public var serviceShow:Bool = false
    //【字段含义】课件下载开关
    @objc public var courseDownShow:Bool = false

    @objc public let view = UIView()
    @objc public let dataSource = NSMutableArray()
    var isTarget:Bool = false
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(self.view)
        self.addTarget(self, action: #selector(self.click), for: .touchUpInside)
        
        self.dataSource.add(self.getConfigurationDict(name: "个人中心", image: "LivingPersonalCenter"))
        self.dataSource.add(self.getConfigurationDict(name: "红"+"包记录" as NSString, image: "redPackRecordShow"))
        self.dataSource.add(self.getConfigurationDict(name: "提"+"现" as NSString, image: "TFwithdraw"))
        self.dataSource.add(self.getConfigurationDict(name: "商"+"城" as NSString, image: "LivingMall"))
        self.dataSource.add(self.getConfigurationDict(name: "客服", image: "LivingCustomerService"))
        self.dataSource.add(self.getConfigurationDict(name: "网络选择", image: "LivingNetworkSelection"))
        self.dataSource.add(self.getConfigurationDict(name: "提问", image: "LivingAsk"))
        self.dataSource.add(self.getConfigurationDict(name: "举报", image: "LivingReport"))
        self.dataSource.add(self.getConfigurationDict(name: "课件下载", image: "生活直播课件下载"))
      
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    @objc func click()
    {
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.view.frame =  CGRect(x: 30, y: 30, width: self.frame.size.width - 30 - 30, height: self.frame.size.width - 30 - 30)
    }
    func getConfigurationDict(name:NSString,image:NSString)->TF_MoreConfigurationButton
    
    {
        let btn = TF_MoreConfigurationButton()
        btn.modelType = name as String
        btn.setImage(UIImage.init(named: image as String), for: .normal)
        btn.setTitle(name as String, for: .normal)
        btn.setTitleColor(UIColor(red: 77/255.0, green: 83/255.0, blue: 88/255.0, alpha: 1), for: .normal)
        btn.titleLabel!.font = UIFont.PingFang(size: 14)
        btn.titleLabel!.textAlignment = .center;
        self.view.addSubview(btn)
        
        if(name == "提问")
        {
            self.questionBtn = btn
        }
       else if(name == "客服")
        {
            self.customerServiceBtn = btn
        }
        return btn;
    }
    @objc public func updateData()
  
    {
        
        for i in stride(from: 0, to: self.dataSource.count ,by: 1) {
            let btn = self.dataSource[i] as! TF_MoreConfigurationButton

            if (btn.modelType == ("商" + "城")) {
                //提问
                if (self.storeShow) {
                    btn.isHidden = false;
                }else{
                    btn.isHidden = true
                }
            
            }
            else if (btn.modelType == "提问") {
                //提问
                if (self.questionShow) {
                    btn.isHidden = false;
                }else{
                    btn.isHidden = true
                }
            
            }
            else if (btn.modelType == "客服") {
                //提问
                if (self.serviceShow) {
                    btn.isHidden = false;
                }else{
                    btn.isHidden = true
                }
            
            }
            else  if (btn.modelType == "网络选择") {
                //线路切换
                if (self.liveLineShow) {
                    btn.isHidden = false;
                }else{
                    btn.isHidden = true
                }
            
            }
            //个人中心
            else  if (btn.modelType == "个人中心") {
                if (self.infoShow) {
                    btn.isHidden = false;
                }else{
                    btn.isHidden = true
                }
            }
            
            //红_包
            else if (btn.modelType == ("红"+"包记录"))
            {
                if (self.redPackRecordShow) {
                    btn.isHidden = false;
                }else{
                    btn.isHidden = true
                }
            }
            //举报
            else if (btn.modelType == "举报") {
               
                if (self.reportShow) {
                    btn.isHidden = false;
                }else{
                    btn.isHidden = true
                }
            }
            //提_现
            else if (btn.modelType == ("提"+"现")) {
            
                if (self.withdrawShow) {
                    btn.isHidden = false;
                }else{
                    btn.isHidden = true
                }
            }
            //课件下载
            else if  (btn.modelType == "课件下载") {
               
                if (self.courseDownShow) {
                    btn.isHidden = false;
                }else{
                    btn.isHidden = true
                }
            }
        }

        let itemWidth:CGFloat = 50 // 宽
        
        let itemHeight:CGFloat = itemWidth + 7 + 20; // 高
        
        let totalColumns:NSInteger = 4; // 每行最大列数（影响到底几个换行）
        self.tempArray.removeAllObjects()
     
        
        for i in stride(from: 0, to: self.dataSource.count ,by: 1) {
            let btn = self.dataSource[i] as! TF_MoreConfigurationButton
            if (btn.isHidden == false) {
                self.tempArray.add(btn)
            }
        }
        // 数据源
        let sourceNumber:NSInteger = self.tempArray.count;
        //// 水平间距
        let margin_X:CGFloat = (self.view.frame.size.width - (itemWidth * CGFloat(totalColumns))) / (CGFloat(totalColumns) - 1)
        
        let margin_Y:CGFloat = 21.5 // 数值间距
        
        for index in stride(from: 0, to: sourceNumber ,by: 1) {

            let itemView = self.tempArray[index] as! TF_MoreConfigurationButton

               // 图片所在行
            let row = index / totalColumns;
             // 图片所在列
            let col = index % totalColumns;
            
         
            let cellX:CGFloat =  CGFloat(col) * (itemWidth + margin_X);
            
            let cellY:CGFloat = CGFloat(row) * (itemHeight + margin_Y);
            
            itemView.frame = CGRect(x: cellX, y: cellY, width: itemWidth, height: itemHeight)
            
           // CGRectMake(cellX,cellY, itemWidth, itemHeight);
            
        
        }
        
    }
    
    @objc public func buttonAddTarget(target:NSObject, action:Selector) {
        
        if (self.isTarget == false) {
            self.isTarget = true
            
            for index in stride(from: 0, to: self.dataSource.count ,by: 1) {
            
              let itemView = self.dataSource[index] as! TF_MoreConfigurationButton
                itemView.addTarget(target, action: action, for: .touchUpInside)
               
            }
        }
    }
    
    @objc public func showConfiguration(value:Bool) {
        UIView.animate(withDuration: 0.5) {
            if (value) {
                self.frame = CGRect(x: 0, y: self.superview!.frame.size.height - 229.5, width: self.superview!.frame.size.width, height: 229.5) //CGRectMake(0,self.superview.frame.size.height -229.5,self.superview.frame.size.width,229.5);
            }else{
                 self.frame = CGRect(x: 0, y: self.superview!.frame.size.height, width: self.superview!.frame.size.width, height: 229.5) //CGRectMake(0,self.superview.frame.size.height,self.superview.frame.size.width,229.5);
            }
            
     
    }
    }
}
